package ged.customizationCode.functions;

import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;

public class LabelPositioning {

	public StackLayout getLayout(ged.Label_attributes label_attributes)
	{
		StackLayout preferredSide = null;
		ged.LabelPosition label_position = label_attributes.getLabel_position();
		switch(label_position) {
		case BOTTOM_LEFT:
			preferredSide = bottomRectangleLabel();
			break;
		case BOTTOM_RIGHT:
			preferredSide = bottomRectangleLabel();
			break;
		case TOP:
			preferredSide = topRectangleLabel();
			break;
		case TOP_LEFT:
			preferredSide = topRectangleLabel();
			break;
		case BOTTOM:
			preferredSide = bottomRectangleLabel();
			break;
		case LEFT:
			preferredSide = leftRectangleLabel();//left
			break;
		case RIGHT:
			preferredSide = rightRectangleLabel();//right
			break;
		case CENTER:
			preferredSide = centerRectangleLabel();
			break;
		default:
			preferredSide = topRectangleLabel(); //default is the top right location
			break;	
		}
		return preferredSide;
	}
	
	public StackLayout centerRectangleLabel()
	{
		StackLayout layout = new StackLayout() {
			  public void layout(IFigure figure) {
				    Rectangle r = figure.getClientArea();
				    List children = figure.getChildren();
				 
				    IFigure child;
				    Dimension d;
				    for (int i = 0; i < children.size(); i++) {
				        child = (IFigure)children.get(i);
				        d = child.getPreferredSize(r.width, r.height);
				        d.width = Math.min(d.width, r.width);
				        d.height = Math.min(d.height, r.height);
				        Rectangle childRect = new Rectangle(
				          d.width/2,
				          r.y + (r.height - d.height)/2,
				          d.width,
				          d.height);
				        child.setBounds(childRect);
				    }
				  }
				};
		return layout;
	}
	
	public StackLayout topRectangleLabel()
	{
		StackLayout layout = new StackLayout() {
			  public void layout(IFigure figure) {
				    Rectangle r = figure.getClientArea();
				    List children = figure.getChildren();
				 
				    IFigure child;
				    Dimension d;
				    for (int i = 0; i < children.size(); i++) {
				        child = (IFigure)children.get(i);
				        d = child.getPreferredSize(r.width, r.height);
				        //find the smallest
				        d.width = Math.min(d.width, r.width);
				        d.height = Math.min(d.height, r.height);
				        Rectangle childRect = new Rectangle(
				          d.width/2,
				          r.y + d.height/2,
				          d.width,
				          d.height);//second parameter could also be r.y
				        child.setBounds(childRect);
				    }
				  }
				};
		return layout;
	}
	
	public StackLayout bottomRectangleLabel()
	{
		StackLayout layout = new StackLayout() {
			  public void layout(IFigure figure) {
				    Rectangle r = figure.getClientArea();
				    List children = figure.getChildren();
				 
				    IFigure child;
				    Dimension d;
				    for (int i = 0; i < children.size(); i++) {
				        child = (IFigure)children.get(i);
				        d = child.getPreferredSize(r.width, r.height);
				        //find the smallest
				        d.width = Math.min(d.width, r.width);
				        d.height = Math.min(d.height, r.height);
				        Rectangle childRect = new Rectangle(
				          d.width/2,
				          r.y + r.height/2 + d.height * 2,
				          d.width,
				          d.height);
				        child.setBounds(childRect);
				    }
				  }
				};
		return layout;
	}
	
	public StackLayout rightRectangleLabel()
	{
		StackLayout layout = new StackLayout()  {
			  public void layout(IFigure figure) {
				    Rectangle r = figure.getClientArea();
				    List children = figure.getChildren();
				 
				    IFigure child;
				    Dimension d;
				    for (int i = 0; i < children.size(); i++) {
				        child = (IFigure)children.get(i);
				        d = child.getPreferredSize(r.width, r.height);
				        d.width = Math.min(d.width, r.width);
				        d.height = Math.min(d.height, r.height);
				        Rectangle childRect = new Rectangle(
				          r.x + r.width/2,
				          r.y + (r.height - d.height)/2,
				          d.width,
				          d.height);
				        child.setBounds(childRect);
				    }
				  }
				};
		return layout;
	}
	
	public StackLayout leftRectangleLabel()
	{
		StackLayout layout = new StackLayout()  {
			  public void layout(IFigure figure) {
				    Rectangle r = figure.getClientArea();
				    List children = figure.getChildren();
				 
				    IFigure child;
				    Dimension d;
				    for (int i = 0; i < children.size(); i++) {
				        child = (IFigure)children.get(i);
				        d = child.getPreferredSize(r.width, r.height);
				        d.width = Math.min(d.width, r.width);
				        d.height = Math.min(d.height, r.height);
				        Rectangle childRect = new Rectangle(
				          r.x + (r.width - d.width)/2,
				          r.y + (r.height - d.height)/2,
				          d.width,
				          d.height);
				        child.setBounds(childRect);
				    }
				  }
				};
		return layout;
	}
}
